package com.dflc.config;

import cn.dev33.satoken.stp.StpUtil;
import com.dflc.entity.vo.BatchLogoutEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Service
@Slf4j
public class LogoutEventHandler {

    @Async
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void handleBatchLogout(BatchLogoutEvent event) {
        event.getUserIds().forEach(userId -> {
            try {
                StpUtil.logout(userId);
                log.info("用户登出成功: {}", userId);
            } catch (Exception e) {
                log.error("用户登出失败: {}", userId, e);
            }
        });
    }
}
